package com.example.travelapplication.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.Room;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.travelapplication.R;
import com.example.travelapplication.activity.DetailActivity;
import com.example.travelapplication.activity.MoreActivity;
import com.example.travelapplication.adapter.AttractionAdapter;
import com.example.travelapplication.dao.AttDao;
import com.example.travelapplication.database.TravelDatabase;
import com.example.travelapplication.entity.Attraction;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public HomeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HomeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onResume() {
        super.onResume();
        list_home = attDao.getAttByCategory(0);
        list_hk = attDao.getAttByCategory(1);
        list_abroad = attDao.getAttByCategory(2);

        adapter_home.setData(list_home);
        adapter_hk.setData(list_hk);
        adapter_abroad.setData(list_abroad);

        adapter_home.notifyDataSetChanged();
        adapter_hk.notifyDataSetChanged();
        adapter_abroad.notifyDataSetChanged();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    private AttractionAdapter adapter_home;
    private AttractionAdapter adapter_hk;
    private AttractionAdapter adapter_abroad;
    private List<Attraction> list_home = new ArrayList<>();
    private List<Attraction> list_hk = new ArrayList<>();
    private List<Attraction> list_abroad = new ArrayList<>();
    private RecyclerView recyclerView_home;
    private RecyclerView recyclerView_abroad;
    private RecyclerView recyclerView_hk;
    private TextView tv_more_home;
    private TextView tv_more_hk;
    private TextView tv_more_abroad;
    private TravelDatabase travelDatabase;
    private AttDao attDao;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        init(view);

        return view;
    }
    private void init(View view){
        travelDatabase = Room.databaseBuilder(getContext(), TravelDatabase.class, "travelDb")
                .allowMainThreadQueries()
                .build();
        attDao = travelDatabase.attDao();

        list_home = attDao.getAttByCategory(0);
        list_hk = attDao.getAttByCategory(1);
        list_abroad = attDao.getAttByCategory(2);

        adapter_home = new AttractionAdapter(list_home, getContext(), new AttractionAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(getContext(), DetailActivity.class);
                intent.putExtra("id", list_home.get(position).attId);
                getActivity().startActivity(intent);
            }
        });

        adapter_hk = new AttractionAdapter(list_hk, getContext(), new AttractionAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(getContext(), DetailActivity.class);
                intent.putExtra("id", list_hk.get(position).attId);
                getActivity().startActivity(intent);
            }
        });

        adapter_abroad = new AttractionAdapter(list_abroad, getContext(), new AttractionAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(getContext(), DetailActivity.class);
                intent.putExtra("id", list_abroad.get(position).attId);
                getActivity().startActivity(intent);
            }
        });

        recyclerView_home = view.findViewById(R.id.rv_home);
        recyclerView_abroad = view.findViewById(R.id.rv_abroad);
        recyclerView_hk = view.findViewById(R.id.rv_hk);

        recyclerView_home.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false));
        recyclerView_abroad.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false));
        recyclerView_hk.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false));

        recyclerView_home.setAdapter(adapter_home);
        recyclerView_abroad.setAdapter(adapter_abroad);
        recyclerView_hk.setAdapter(adapter_hk);

        tv_more_home = view.findViewById(R.id.tv_more_home);
        tv_more_hk = view.findViewById(R.id.tv_more_hk);
        tv_more_abroad = view.findViewById(R.id.tv_more_abroad);

        tv_more_home.setOnClickListener(listener);
        tv_more_hk.setOnClickListener(listener);
        tv_more_abroad.setOnClickListener(listener);
    }

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), MoreActivity.class);
            startActivity(intent);
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        travelDatabase.close(); //关闭数据库
    }

}